<?php if (!defined('LEGAL_INNER_USE')){die('ILLEGAL USING!');}?>
<?php
$per_page = 20;
$page = isset($_GET['page'])?$_GET['page']*1:1;
$page = ($page<1)?1:$page;

$breadcrumb = '';

if (isset($_GET['cateId'])){
	$cateId = $_GET['cateId']*1;
	$catesPath = $categories -> ExtTreeData2($catesSorted);
	if (isset($catesPath[$cateId]) && !empty($catesPath[$cateId])){
		#print_r($catesPath[$cateId]);
		foreach($catesPath[$cateId] as $v){
			$breadcrumb .= '<li><span class="divider">/</span><a href="'.makeUrl("products", "cateId=".$v["cat_id"]).'">'.$v['cat_name'].'</a></li>';
		}
	} else {
		$cateId = 0;
	}
} else {
	$cateId = 0;
}

if ($cateId>0){
	$addTitle = isset($catesPath[$cateId])?end($catesPath[$cateId]):false;
	$addTitle = isset($addTitle['cat_name'])?trim($addTitle['cat_name']):false;
	unset($catesPath);
} else {
	$addTitle = '所有产品';
}

#产品信息
if ($cateId == 0){
	$sql = "SELECT COUNT(*) AS t FROM ".DB_PRE.DB_PRE_ECSHOP."goods WHERE is_on_sale=1";
	#计数
	$totals = $db -> execute($sql) -> fields['t'];
} else {
	$subCateIds = diy_execute("SELECT cat_id FROM ".DB_PRE.DB_PRE_ECSHOP."category WHERE parent_id=$cateId", 'cat_id');
	$subCateIds[] = $cateId;
	#print_r($subCateIds);exit;
	$sql = "SELECT COUNT(*) AS t FROM ".DB_PRE.DB_PRE_ECSHOP."goods WHERE cat_id IN (".implode(',', $subCateIds).")";
	#echo $sql;
	$totals = $db -> execute($sql) -> fields['t'];
}
#echo $totals;exit;

$total_pages = ceil($totals/$per_page);
$page = ($page>$total_pages)?$total_pages:$page;
$page = ($page<1)?1:$page;

if ($cateId == 0){
	$sql = "SELECT goods_id, goods_name, shop_price, goods_thumb, cat_id FROM ".DB_PRE.DB_PRE_ECSHOP."goods ORDER BY sort_order DESC, goods_id DESC LIMIT ".(($page-1)*$per_page).", $per_page";
} else {
	$sql = "SELECT goods_id, goods_name, shop_price, goods_thumb FROM ".DB_PRE.DB_PRE_ECSHOP."goods WHERE cat_id IN (".implode(',', $subCateIds).") ORDER BY sort_order DESC, goods_id DESC LIMIT ".(($page-1)*$per_page).", $per_page";
}

$goods = diy_execute($sql);
for ($i=0; $i<(sizeof($goods))%4; $i++){
	$goods[] = false;
}
#echo sizeof($goods);exit;
